<?php




class Y {
    public static $controller;       // controller name
    public static $controllerObj;    // controller object
    public static $action;

    public static $htmlFile = '';
    public static $tplFile = '';
    public static $tpl;
    private static $cacheId;


    private static $logined = false;

    public function __construct() {
    //error_reporting(0);
        self::checkLogin();
        self::setCtlAct();
        self::setTemplate();

        self::run();
    }

    public function __destruct() {
    //$cacheId = self::$controller . self::$action . $_GET['domain'];

        self::$tpl->set('tplFile', self::$tplFile);
        self::$tpl->render(self::$htmlFile, self::$cacheId);
    }



    protected static function setTemplate() {
        global $INCLUDE_PATH;
        require_once($INCLUDE_PATH . 'include/DyTemplate.php');
        self::$tpl = new DyTemplate();
        self::$tpl->left_delimiter = "<{";
        self::$tpl->right_delimiter = "}>";
    }




  /*
   * set controller & action name by URL params
   * format:   http[/https]://host/?ctl=[ctl]&act=[act][&other params if you want...]
   *
   * if [ctl] param is not assigned, then controller name will be 'index'
   * if [act] param is not assigned, then action name will be 'default'
   *
   */
    protected function setCtlAct() {
        $ctl = & $_GET['ctl'];
        $act = & $_GET['act'];

        if(self::$logined != 1  or $_COOKIE['adminlogintime']<(time()-20*60) ) {
            setcookie("adminlogintime",time());
            self::$controller = "index";

            if (!in_array($act, array("login", "stations","deptLogin")) OR $ctl != "post" )
                self::$action = 'login';

        } else {

            self::$controller = $ctl ? strtolower($ctl) : 'index';
            self::$action = $act ? strtolower($act) : 'default';
        }
        if ($_REQUEST['key']) {
            self::$controller = $ctl ? strtolower($ctl) : 'index';
            self::$action = $act ? strtolower($act) : 'default';
        }
        $_GET['ctl'] = self::$controller;
        $_GET['act'] = self::$action;

        if ('' === self::$htmlFile) {
            self::$htmlFile = 'index.html';
        }
        if ('' === self::$tplFile) {
            self::$tplFile = self::$controller.'/'.self::$action;
        } else {
            self::$tplFile = self::$tplFile;
        }

    }

    public function run() {
        global $INCLUDE_PATH;
        $ctlFile = APP_ROOT . 'app/controller/' . self::$controller . '.php';

        if (!file_exists($ctlFile)) {
            self::$htmlFile  = '404.html';
        } else {

            require_once $ctlFile;

            $ctlClass = ucfirst(self::$controller) . 'Controller';

            self::$controllerObj = new $ctlClass();

            // set header for controller
            self::$controllerObj->setPageHeader();



            $method = 'action' . ucfirst(self::$action);

            self::$controllerObj->$method();
        }
    }

    function checkLogin() {
        require_once './include/auth/DyAuth.php';
        if ($_COOKIE['adminname'] or $_REQUEST['key']) {
            self::$logined = 1;
        }
    // echo self::$logined;
    }
}



?>
